<?php
class ModelArticleArticle extends Model {
	public function addArticle($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "article SET category_id = '" . (int)$data['category_id'] . "', `top` = '" . (isset($data['top']) ? (int)$data['top'] : 0) . "', `hot` = '" . (isset($data['hot']) ? (int)$data['hot'] : 0) . "', `title` = '" . $data['title'] . "', sort_order = '" . (int)$data['sort_order'] . "', status = '" . (int)$data['status'] . "', date_modified = NOW(), date_added = NOW()");
	
		$article_id = $this->db->getLastId();
		
		if (isset($data['image'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "article SET image = '" . $this->db->escape($data['image']) . "' WHERE article_id = '" . (int)$article_id . "'");
		}
		
		if (isset($data['article_description'])) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "article_description SET article_id = '" . (int)$article_id . "', content = '" . $this->db->escape($data['article_description']['content']) . "', meta_keyword = '" . $this->db->escape($data['article_description']['meta_keyword']) . "'");
		}
        
		$this->cache->delete('article');
	}
	
	public function editArticle($article_id, $data) {
		$this->db->query("UPDATE " . DB_PREFIX . "article SET category_id = '" . (int)$data['category_id'] . "', `top` = '" . (isset($data['top']) ? (int)$data['top'] : 0) . "', `hot` = '" . (isset($data['hot']) ? (int)$data['hot'] : 0) . "', `title` = '" . $data['title'] . "', sort_order = '" . (int)$data['sort_order'] . "', status = '" . (int)$data['status'] . "', date_modified = NOW() WHERE article_id = '" . (int)$article_id . "'");
		
        if (isset($data['image'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "article SET image = '" . $this->db->escape($data['image']) . "' WHERE article_id = '" . (int)$article_id . "'");
		}

		$this->db->query("DELETE FROM " . DB_PREFIX . "article_description WHERE article_id = '" . (int)$article_id . "'");

		if (isset($data['article_description'])) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "article_description SET article_id = '" . (int)$article_id . "', content = '" . $this->db->escape($data['article_description']['content']) . "', meta_keyword = '" . $this->db->escape($data['article_description']['meta_keyword']) . "'");
		}
		
		$this->cache->delete('article');
	}
	
	public function deleteArticle($article_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "article WHERE article_id = '" . (int)$article_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "article_description WHERE article_id = '" . (int)$article_id . "'");
		
		$this->cache->delete('article');
	} 

	public function getArticle($article_id) {
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "article WHERE article_id = '" . (int)$article_id . "'");
		
		return $query->row;
	} 
	
	public function getArticles() {
		$article_data = $this->cache->get('article');
	
		if (!$article_data) {
			$article_data = array();
		
			$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "article a LEFT JOIN " . DB_PREFIX . "article_description ad ON (a.article_id = ad.article_id)  ORDER BY a.sort_order, a.title ASC");

			foreach ($query->rows as $result) {
				$article_data[] = array(
					'article_id'  => $result['article_id'],
					'category_id' => $result['category_id'],
					'title'       => $result['title'],
					'content'       => $result['content'],
					'content'       => $result['content'],
					'categoryname'=> $this->getPath($result['category_id']),
					'status'  	  => $result['status'],
					'sort_order'  => $result['sort_order']
				);
			}	
			$this->cache->set('article', $article_data);
		}
		
		return $article_data;
	}
	
	public function getPath($category_id) {
		$query = $this->db->query("SELECT name, parent_id FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) WHERE c.category_id = '" . (int)$category_id. "' ORDER BY c.sort_order, cd.name ASC");
		
		$category_info = $query->row;
		
		if ($category_info['parent_id']) {
			return $this->getPath($category_info['parent_id']) . ' &gt; ' . $category_info['name'];
		} else {
			return $category_info['name'];
		}
	}
    
	public function getArticleDescriptions($article_id) {
		$article_description_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "article_description WHERE article_id = '" . (int)$article_id . "'");
		
		foreach ($query->rows as $result) {
			$article_description_data = array(
				'content'          => $result['content'],
				'meta_keyword'     => $result['meta_keyword']
			);
		}
		
		return $article_description_data;
	}	
		
	public function getTotalArticles() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "article");
		
		return $query->row['total'];
	}	
		
	public function getTotalArticlesByImageId($image_id) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "article WHERE image_id = '" . (int)$image_id . "'");
		
		return $query->row['total'];
	}	
}
?>